<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>权限管理</title>
    <meta charset="utf-8">
    <%@ include file="/WEB-INF/pages/common/common.jsp" %>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/md5.js"></script>
    <style type="text/css">
        .data_tree {
            width: 580px;
            height: 455px;
            margin: 5px;
            padding: 10px;
            float: left;
            border: 1px solid #5f5f5f;
            overflow: auto;
        }

        label {
            display: inline-block;
            width: 100px;
            text-align: right;
            margin-top: 5px;
            margin-right: 10px;
            margin-left: 10px;
            font-size: 14px;
        }

        input {
            width: 300px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
<div id="data_left" class="data_tree">
    <div id="dataTools" style="margin-bottom: 20px;">
        <a id="add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="javascript:save(1);">新增</a>
        <a id="save" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="javascript:save(2);">修改</a>
        <a id="remove" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="javascript:remove();">删除</a>
    </div>
    <ul id="root_tree" class="easyui-tree"></ul>
</div>

<div id="data_right" class="data_tree" style="display:none">
    <form id="saveData" method="post">
        <input type="hidden" id="id" name="id"/>
        <input type="hidden" id="parentId" name="parentId" class="easyui-validatebox" data-options="required:true,invalidMessage:'不能为空'"/>
        <div>
            <label>权限名：</label>
            <input id="purName" type="text" class="easyui-validatebox easyui-textbox" name="purName" required="true"
                   data-options="required:true,validType:'length[3,20]',invalidMessage:'至少3个字符'"/>
        </div>
        <div>
            <label>权限地址：</label>
            <input id="purUrl" type="text" class="easyui-validatebox easyui-textbox" name="purUrl"
                   required="true" data-options="required:true,invalidMessage:'不能为空'"/>
        </div>
        <div>
            <label>排序：</label>
            <input id="orderNum" type="text" class="easyui-validatebox easyui-numberbox" name="orderNum"required="true" data-options="required:true,invalidMessage:'不能为空'"/>
        </div>
        <div>
            <label>是否要登陆：</label>
            <select id="login" name="login" class="easyui-validatebox easyui-combobox" style="width:200px;"
                    data-options="editable:false,panelHeight:'auto',required:true,invalidMessage:'不能为空'">
                <option value="0">不登陆</option>
                <option value="1">登陆</option>
            </select>
        </div>
        <div>
            <label>是否有效：</label>
            <select id="state" name="state" class="easyui-validatebox easyui-combobox" style="width:200px;"
                    data-options="editable:false,panelHeight:'auto',required:true,invalidMessage:'不能为空'">
                <option value="0">有效</option>
                <option value="1">无效</option>
            </select>
        </div>
        <div>
            <label>权限类型：</label>
            <select id="purType" name="purType" class="easyui-validatebox easyui-combobox" style="width:200px;"
                    data-options="editable:false,panelHeight:'auto',required:true,invalidMessage:'不能为空'">
                <option value="1">菜单</option>
                <option value="2">内部页面</option>
                <option value="3">内部接口</option>
                <option value="4">外部页面</option>
                <option value="5">外部接口</option>
            </select>
        </div>
        <div>
            <label>说明：</label>
            <input id="purDescribe" type="text" name="purDescribe" class="easyui-textbox" maxlength="255"
                   data-options="multiline:true" style="width:300px;height:100px"/>
        </div>
        <div id="dialog_tool" style="margin-top: 30px;margin-left: 180px;">
            <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="doSaveData()"
               style="width:90px">保 存</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
               onclick="javascript:$('#data_right').hide()" style="width:90px">取 消</a>
        </div>
    </form>
</div>
</body>
<script type="text/javascript">

    window.onload = function () {
        initData();
    }

    function initData() {
        $.ajax({
            type: "GET",
            async: false,
            cache: false,
            dataType: "json",
            contentType: "application/x-www-form-urlencoded",
            url: "${pageContext.request.contextPath}/admin/purview/queryAllPurview",
            success: function (resp) {
                if (resp.code != 0) {
                    $.messager.alert('Warning', '执行出错了,state:' + resp.msg);
                    return
                }
                $("#root_tree").tree({
                    data: resp.data,
                    lines: true,
                });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.alert('Warning', '执行出错了,state:' + textStatus);
            }
        });
    }

    function doSaveData() {
        $('#saveData').form('submit', {
            url: '${pageContext.request.contextPath}/admin/purview/save',
            onSubmit: function () {
                return $(this).form('enableValidation').form('validate');
            },
            success: function (data) {
                var respData = JSON.parse(data);
                if (respData.code == 0) {
                    $.messager.show({
                        title:'消息提示',
                        msg:'操作成功！',
                        timeout:3000,
                        showType:'slide',
                        width:200,
                        height:100
                    });
                    $('#data_right').hide()
                    initData();
                } else {
                    $.messager.alert('保存失败', respData.msg, 'info');
                }
            }
        });
    }

    function save(type) {
        var node = $("#root_tree").tree('getSelected');
        if (node) {
            if (type == 2) {
                if (node.id == 0) {
                    $.messager.alert('校验失败', '根节点不能修改', 'info');
                    return
                }
                var params = {
                    id: node.id
                }
                $.ajax({
                    type: "GET",
                    async: false,
                    cache: false,
                    dataType: "json",
                    contentType: "application/x-www-form-urlencoded",
                    data: params,
                    url: "${pageContext.request.contextPath}/admin/purview/queryPurview",
                    success: function (resp) {
                        if (resp.code != 0) {
                            $.messager.alert('Warning', '执行出错了,state:' + resp.msg);
                            return
                        }
                        $('#saveData').form('load', resp.data);
                        $("#data_right").show();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.messager.alert('Warning', '执行出错了,state:' + textStatus);
                    }
                });
            } else {
                $('#saveData').form('clear');
                $("#parentId").val(node.id);
                $("#data_right").show();
            }
        } else {
            $.messager.alert('校验失败', '请选择一条记录', 'info');
        }
    }

    function remove() {

        var row = $("#root_tree").tree('getSelected');
        if (row) {
            $.messager.confirm('确认操作', '确认删除这条记录吗？', function (r) {
                if (r) {
                    var url = '${pageContext.request.contextPath}/admin/purview/del';
                    $.ajax({
                        type: 'POST',
                        url: url,
                        dataType: 'json',
                        data: {id: row.id},
                        success: function (data) {
                            if (data.code == 0) {
                                $.messager.show({
                                    title:'消息提示',
                                    msg:'操作成功！',
                                    timeout:3000,
                                    showType:'slide',
                                    width:200,
                                    height:100
                                });
                                initData();
                            } else {
                                $.messager.alert('删除失败', data.msg, 'info');
                            }
                        }
                    });
                }
            });
        } else {
            $.messager.alert('校验失败', '请选择一条记录', 'info');
        }
    }
</script>
</html>
